package com.example.contact_recover.util;

import com.alibaba.fastjson.JSON;
import com.example.contact_recover.models.ContactsInfo;
import com.example.contact_recover.models.Message;
import com.example.contact_recover.models.User;

import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
// 访问服务器的工具类,
// 使用了实现会调接口的形式 实现了 工具类的稳定性 依赖接口的形式
public class NetUtil {
    private static final String SERVER = "http://192.168.1.101:8888";
    public static final String SERVER_URL = SERVER + "/user/login";
    public static final String SERVER_PULL = SERVER;

    // 用户向服务器提交数据 自己实现 callBack
    public static void Commit2Server(String url, String phone, String passwd, Callback callback) {
        OkHttpClient client = new OkHttpClient();

        // 构造请求体...
        MultipartBody multipartBody = new MultipartBody.Builder().
                setType(MultipartBody.FORM).
                addFormDataPart("phone", phone).
                addFormDataPart("passwd", passwd).
                build();

        //  post请求方法
        Request request = new Request.Builder().
                url(url).
                post(multipartBody).
                build();

        Call call = client.newCall(request);

        call.enqueue(callback);
    }


    // 从服务器拉取数据
    public static void pull2Server(String url, Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().
                get().
                url(url)
                .build();

        client.newCall(request).enqueue(callback);

    }

    // 将 用户的联系人备份到 服务器
    public static void push2Server(String url, User user, Callback callback) {

        OkHttpClient client = new OkHttpClient();

        String s = JSON.toJSONString(user.getContactsList());

        System.out.println("list = " + s);
        // 构造请求体...
        MultipartBody.Builder builder = new MultipartBody.Builder().
                setType(MultipartBody.FORM).
                addFormDataPart("phone", user.getPhone()).
                addFormDataPart("passwd", user.getPasswd());

        for (ContactsInfo info : user.getContactsList()) {
            builder.addFormDataPart("contacts", JSON.toJSONString(info));
        }

        MultipartBody multipartBody = builder.build();
        //  post请求方法
        Request request = new Request.Builder().
                url(url).
                post(multipartBody).
                build();

        Call call = client.newCall(request);

        call.enqueue(callback);
    }


    // 将 短信备份到服务器
    public static void push2Server(String url, User user, List<Message> messageList, Callback callback) {

        OkHttpClient client = new OkHttpClient();

        String s = JSON.toJSONString(user.getContactsList());

        System.out.println("list = " + s);
        // 构造请求体...
        MultipartBody.Builder builder = new MultipartBody.Builder().
                setType(MultipartBody.FORM).
                addFormDataPart("phone", user.getPhone()).
                addFormDataPart("passwd", user.getPasswd());

        for (Message msg  : messageList) {
            builder.addFormDataPart("messages", JSON.toJSONString(msg ));
        }

        MultipartBody multipartBody = builder.build();
        //  post请求方法
        Request request = new Request.Builder().
                url(url).
                post(multipartBody).
                build();

        Call call = client.newCall(request);

        call.enqueue(callback);
    }


}
